import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from utils.error_handle import ui_exception_record
from utils.utility_method import driver_path
from utils.logger import logger


class BasePage:
    _BASE_URL = "https://oms.8zyun.cn/login?redirect=%2Fagentssy%2Findex"

    def __init__(self, driver: WebDriver = None):
        if driver:
            # 复用_driver
            self._driver = driver
        else:
            # options = webdriver.ChromeOptions()
            # options.add_argument('--headless')
            # options.add_argument('start-maximized')
            # options.add_argument("--window-size=1920,1050")
            # # 给root执行权限
            # options.add_argument("--no-sandbox")
            # 新版selenium启动的时候好像会优先去下载driver，执行脚本后等很久才会启动浏览器，为了解决这个问题，直接使用本地的chromedriver，提高启动速度
            from selenium.webdriver.chrome.service import Service as ChromeService
            chrome_options = Options()
            chrome_options.add_argument("--window-size=1960,1080")
            chrome_options.add_argument("--headless")
            self._driver = webdriver.Chrome(service=ChromeService(driver_path), options=chrome_options)

        # self._driver = webdriver.Chrome(service=ChromeService(driver_path))

            self._driver.implicitly_wait(10)
            self._driver.maximize_window()
            # zoom_out = "document.body.style.zoom='0.7'"
            # self._driver.execute_script(zoom_out)
            self._driver.get(self._BASE_URL)

    @ui_exception_record
    def find(self, by, locator=None) -> WebElement:
        """查找元素"""
        logger.info(f"查找元素：by>{by}, locator>{locator}")
        if locator:
            ele = self._driver.find_element(by, locator)
        else:
            ele = self._driver.find_element(*by)
        return ele

    @ui_exception_record
    def finds(self, by, locator=None) -> list[WebElement]:
        """查找元素"""
        logger.info(f"查找元素：by>{by}, locator>{locator}")
        if locator:
            return self._driver.find_elements(by, locator)
        else:
            return self._driver.find_elements(*by)

    def find_and_click(self, by, locator=None):
        logger.info(f"查找到之后点击")
        self.find(by, locator).click()

    def find_and_send(self, text, by, locator=None):
        ele = self.find(by, locator)
        ele.click()
        logger.info(f"查找到之后完成输入：{text}")
        ele.clear()
        ele.send_keys(text)

    def wait_for_visible(self, locator):
        """显示等待元素可见"""
        logger.info("显式等待等到元素可见")
        return WebDriverWait(self._driver, 10, 0.5). \
            until(expected_conditions.visibility_of_element_located(locator))

    def wait_for_clickable(self, locator):
        """显示等待元素点击"""
        logger.info("显式等待等到元素可见")
        return WebDriverWait(self._driver, 10, 0.5). \
            until(expected_conditions.element_to_be_clickable(locator))

    def get_text(self, by, locator=None):
        """获取文本内容"""
        logger.info("获取元素的文本信息")
        return self.find(by, locator).text

    def quit(self):
        return self._driver.quit()

    def set_time(self, t: int|float):
        # 设置强制等待时间
        time.sleep(t)

    def get_attribute(self, by, locator, attribute):
        logger.info(f"获取元素({by, locator})的属性：{attribute}")
        return self.find(by, locator).get_attribute(attribute)

